home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / include / option.h < prev    next >
C/C++ Source or Header  |  1991-01-28  |  5KB  |  146 lines

  1. /*
  2.  * option.h --
  3.  *    This defines the Option type and the interface to the
  4.  *    Opt_Parse library call that parses command lines.
  5.  *
  6.  * Copyright 1988, 1991 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  * $Header: /sprite/src/lib/include/RCS/option.h,v 1.8 91/01/28 12:30:55 kupfer Exp $ SPRITE (Berkeley)
  16.  */
  17.  
  18. #ifndef _OPTION
  19. #define _OPTION
  20.  
  21. #include <cfuncproto.h>
  22.  
  23. /*
  24.  * An array of option descriptions (type Option) is passed into the
  25.  * routine which interprets the command line.  Each option description
  26.  * includes the key-string that indicates the option, a type for the option,
  27.  * the address of an associated variable, and a documentation message
  28.  * that is printed when the command is invoked with a single argument
  29.  * of '?'
  30.  */
  31.  
  32. typedef struct Option {
  33.     int        type;        /* Indicates option type;  see below */
  34.     char    *key;        /* The key string that flags option */
  35.     char *    address;    /* Address of variable to modify */
  36.     char    *docMsg;    /* Documentation message */
  37. } Option;
  38. /*
  39.  * Values for type:
  40.  *
  41.  *    OPT_CONSTANT(val) -    if the flag is present then set the
  42.  *                associated (integer) variable to val.
  43.  *                Val must be a non-negative integer.
  44.  *    OPT_TRUE -        if the flag is present then set the
  45.  *                associated (integer) variable to TRUE (1).
  46.  *    OPT_FALSE -        if the flag is present then set the
  47.  *                associated (integer) variable to FALSE (0).
  48.  *    OPT_INT -        if the flag is present then the next argument
  49.  *                on the command line is interpreted as an
  50.  *                integer and that value is assigned to the
  51.  *                options associated variable.
  52.  *    OPT_STRING -        if the flag is present then the next argument
  53.  *                on the command line is copied into the string
  54.  *                variable associated with the option.
  55.  *    OPT_REST -        if the flag is present, inhibit processing of
  56.  *                later options, so that they're all returned
  57.  *                to the caller in argv.  In addition, set the
  58.  *                associated variable to the index of the first
  59.  *                of these arguments in the returned argv.
  60.  *                This permits a program to allow a flag to
  61.  *                separate its own options from options it will
  62.  *                pass to another program.
  63.  *    OPT_FLOAT -        if the flag is present then the next argument
  64.  *                on the command line is interpreted as a
  65.  *                "double" and that value is assigned to the
  66.  *                option's associated variable.
  67.  *    OPT_TIME -        if the flag is present then the next argument 
  68.  *                on the command line is interpreted as a date 
  69.  *                and time.  The corresponding time value 
  70.  *                (number of seconds past the epoch) is assigned
  71.  *                to the option's associated variable.
  72.  *    OPT_FUNC -        if the flag is present, pass the next argument
  73.  *                to "address" as a function. The function
  74.  *                should be declared:
  75.  *                    int
  76.  *                    func(optString, arg)
  77.  *                    char     *optString;
  78.  *                    char    *arg;
  79.  *                    Func should return non-zero if the argument
  80.  *                was consumed or zero if not.  "optString" is
  81.  *                the option key string that caused the
  82.  *                function to be called and "arg" is the next
  83.  *                argument (if there is no next argument then
  84.  *                "arg" will be NULL).
  85.  *    OPT_GENFUNC -        if the flag is present, pass the remaining
  86.  *                arguments and the number of arguments to
  87.  *                "address" as a function. The function should
  88.  *                be declared:
  89.  *                    int
  90.  *                    func(optString, argc, argv)
  91.  *                    char *optString;
  92.  *                    int argc;
  93.  *                    char **argv;
  94.  *                and should return the new number of arguments
  95.  *                left in argv.  argv should have been shuffled
  96.  *                to eliminate the arguments func consumed.
  97.  *    OPT_DOC -        a dummy entry. Exists mostly for its
  98.  *                documentation string.  As an additional side
  99.  *                effect, if its key string an argument,
  100.  *                Opt_Parse will treat it like a question mark
  101.  *                (i.e. print out the program's usage and exit).
  102.  */
  103.  
  104. #define OPT_CONSTANT(val)    ((int) val)
  105. #define OPT_FALSE        0
  106. #define OPT_TRUE        1
  107. #define OPT_INT            -1
  108. #define OPT_STRING        -2
  109. #define OPT_REST        -3
  110. #define OPT_FLOAT        -4
  111. #define OPT_FUNC        -5
  112. #define OPT_GENFUNC        -6
  113. #define OPT_DOC            -7
  114. #define OPT_TIME        -8
  115.  
  116. /*
  117.  * Flag values for Opt_Parse:
  118.  *
  119.  * OPT_ALLOW_CLUSTERING -    Permit many flags to be clustered under
  120.  *                a single "-".  In otherwords, treat
  121.  *                "foo -abc" the same as "foo -a -b -c".
  122.  * OPT_OPTIONS_FIRST    -    Stop parsing if something other than an
  123.  *                option (starting with a hyphen) is encountered.
  124.  */
  125.  
  126. #define OPT_ALLOW_CLUSTERING    1
  127. #define OPT_OPTIONS_FIRST    2
  128.  
  129. /*
  130.  * Exported procedures:
  131.  */
  132.  
  133. int Opt_Parse _ARGS_ ((int argc, char *argv[], Option *optionArray, 
  134.                int numOptions, int flags));
  135.  
  136. void Opt_PrintUsage _ARGS_ ((char *commandName, Option *optionArray,
  137.                  int numOptions));
  138.  
  139. /*
  140.  * Macro to determine size of option array:
  141.  */
  142.  
  143. #define Opt_Number(optionArray)    (sizeof(optionArray)/sizeof((optionArray)[0]))
  144.  
  145. #endif /* _OPTION */
  146.